package com.cml.topicsystem.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.SocketTimeoutException;

/**
 * @author cml
 * @create 2021/6/4 8:35
 */
public class MyInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("进入pre拦截器" + request.getRequestURI() + " " + request.getMethod());
        try {
            Object user = request.getSession().getAttribute("user");
            System.out.println("seID:" + request.getSession().getId() + " user:"+user );
            if(user != null){

                return true;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        System.out.println("==============");
        response.sendRedirect("/user/logout");
        return false;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }
}
